
import { globalUtils } from '../../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';
import uiData from 'ui-data';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewScope('example2-search', function (scope) {

  // 点击查询按钮的回调函数
  scope.clickSearchBtnFn = function () {
    var jqTable = scope.getJqTable();
    var view = scope.getAppView();

    var model = view.getDataModel();
    var data = model.getData('searchField.*');
    data = uiData.utils.unprefix(data, 'searchField.');

    uiUtils.dataGrid.newQuery(jqTable, {
      customQuery: data
    });
  };

  // 点击新建按钮的回调函数
  scope.clickCreateBtnFn = function (e) {
    var url = globalUtils.formatString('app/example2/new');

    Pure.app.pushView(url);
  };

  // 点击批量删除按钮的回调函数
  scope.clickBatchDeleteBtnFn = function () {
    var jqTable = scope.getJqTable();
    var view = scope.getAppView();
    var recordIds = uiUtils.dataGrid.getIdSelections(jqTable, 'id');

    var data = {
      ids: recordIds
    };

    uiUtils.dialog.showConfirmBox('${t("ui.example.example2.msg.confirmBatchDeleteReocrd")}', function () {
      // 调用批量删除记录的接口
      view.callService('api/example/batchDeleteRecordByIds', data)
        .waitResult(function (result, textStatus, jqXHR) {
          if (result.success) {
            Pure.app.showMessage('${t("ui.example.example2.msg.deleteRecordSuccess")}');
            // 刷新表格
            uiUtils.dataGrid.refresh(jqTable);
          } else {
            Pure.app.showError('${t("ui.example.example2.msg.deleteRecordError")}');
          }
        });
    });
  };

  // 点击查看操作的回调函数
  scope.clickViewOperationFn = function (e, value, row, index) {
    var id = row.id;
    var url = globalUtils.formatString('app/example2/view/{0}', id);

    Pure.app.pushView(url);
  };

  // 点击编辑操作的回调函数
  scope.clickEditOperationFn = function (e, value, row, index) {
    var id = row.id;
    var url = globalUtils.formatString('app/example2/edit/{0}', id);

    Pure.app.pushView(url);
  };

  // 点击删除操作的回调函数
  scope.clickDeleteOperationFn = function (e, value, row, index) {
    var jqTable = scope.getJqTable();
    var view = scope.getAppView();

    var data = {
      id: row.id
    };

    uiUtils.dialog.showConfirmBox('${t("ui.example.example2.msg.confirmDeleteReocrd")}', function () {
      // 调用删除记录的接口
      view.callService('api/example/deleteRecordById', data)
        .waitResult(function (result, textStatus, jqXHR) {
          if (result.success) {
            Pure.app.showMessage('${t("ui.example.example2.msg.deleteRecordSuccess")}');
            // 刷新表格
            uiUtils.dataGrid.refresh(jqTable);
          } else {
            Pure.app.showError('${t("ui.example.example2.msg.deleteRecordError")}');
          }
        });
    });
  };

});

/* SOURCE-CODE-END */
